package net.mysoft.framework.base;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.domain.BaseEntity;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;
@Repository("baseDao")
public class BaseDaoImpl<T extends BaseEntity> implements BaseDao<T> {
	@Resource
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	public Serializable save(T o) {
		String id = o.getId();
		if(StringUtils.isNotBlank(id)) o.setModifyDate(new Date());
		else o.setCreateDate(new Date());
		return this.getCurrentSession().save(o);
	}

	public void update(T o) {
		this.getCurrentSession().update(o);
	}

	public void saveOrUpdate(T o) {
		this.getCurrentSession().saveOrUpdate(o);
	}
	
	
	public void saveOrUpdate(Collection<T> entities) {
		for(T t:entities){
			saveOrUpdate(t);
		}
	}
	
	public void delete(T o) {
		this.getCurrentSession().delete(o);
	}

	public void delete(Collection<T> entities) {
		for(T t:entities){
			delete(t);
		}
	}
	
	public T load(Class<T> c, Serializable id) {
		return (T) this.getCurrentSession().load(c, id);
	}
	
	public T get(Class<T> c, Serializable id) {
		return (T) this.getCurrentSession().get(c, id);
	}

	public T get(String hql, Object[] param) {
		List<T> l = this.find(hql, param);
		if (l != null && l.size() > 0) {
			return l.get(0);
		} else {
			return null;
		}
	}

	public T get(String hql, List<Object> param) {
		List<T> l = this.find(hql, param);
		if (l != null && l.size() > 0) {
			return l.get(0);
		} else {
			return null;
		}
	}
	
	public List<T> find(String hql) {
		return this.getCurrentSession().createQuery(hql).list();
	}

	public List<T> find(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return q.list();
	}

	public List<T> find(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return q.list();
	}

	public List<T> find(String hql, Object[] param, Integer page, Integer rows) {
		if (page == null || page < 1) {
			page = 1;
		}
		if (rows == null || rows < 1) {
			rows = 10;
		}
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}

	public List<T> find(String hql, List<Object> param, Integer page, Integer rows) {
		if (page == null || page < 1) {
			page = 1;
		}
		if (rows == null || rows < 1) {
			rows = 10;
		}
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}

	public Long count(String hql) {
		return (Long) this.getCurrentSession().createQuery(hql).uniqueResult();
	}

	public Long count(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return (Long) q.uniqueResult();
	}

	public Long count(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return (Long) q.uniqueResult();
	}
	
	public Integer executeHql(String hql) {
		return this.getCurrentSession().createQuery(hql).executeUpdate();
	}

	public Integer executeHql(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return q.executeUpdate();
	}

	public Integer executeHql(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return q.executeUpdate();
	}
	//SQL语句执行
	public List queryBySql(String sql) {    
       // List<ToDoInfo[]> list = this.getCurrentSession().createSQLQuery(sql).addEntity(ToDoInfo.class).list();    
        List<Object[]> list = this.getCurrentSession().createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();  
        
       // List<Object[]>  list1=this.getCurrentSession().getNamedQuery("{getUserList(0000)}").setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();//up_my_bill_inte
        //List<Object[]>  list1=this.getCurrentSession().getNamedQuery("{call getUserList(0000)}").setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();//up_my_bill_inte
        
        return list;    
    }    
        
    public int excuteBySql(String sql)    
    {    
        int result ;    
        SQLQuery query = this.getCurrentSession().createSQLQuery(sql);    
        result = query.executeUpdate();    
        return result;    
    } 
    
    public List<T> queryPageByHql(String hql, int pageNow, int pageSize) {
    	return queryPageByHql(hql, null, pageNow, pageSize);
    }
    
    public List<T> queryPageByHql(String hql, Map<String, Object> params, int pageNow, int pageSize) {
    	Query query = getCurrentSession().createQuery(hql);
    	if(params!=null&&params.size()>0) {
    		for(Map.Entry<String, Object> entry : params.entrySet()) {
        		query.setParameter(entry.getKey(), entry.getValue());
        	}
    	}
    	return query.setFirstResult((pageNow-1)*pageSize).setMaxResults(pageSize).list();
    }

}
